ARG SW_VERSION
ARG COMPOSER_VERSION
ARG timezone
ARG ALPINE_VERSION=3.12

FROM alpine:${ALPINE_VERSION}

LABEL maintainer="lujihong" version="1.0" license="MIT"

# trust this project public key to trust the packages.
ADD https://php.hernandev.com/key/php-alpine.rsa.pub /etc/apk/keys/php-alpine.rsa.pub

##
# ---------- env settings ----------
##
ENV SW_VERSION=${SW_VERSION:-"v4.8.12"} \
    # composer
    COMPOSER_VERSION=${COMPOSER_VERSION:-"2.0.14"} \
    # alpine version
    ALPINE_VERSION=${ALPINE_VERSION:-"3.12"} \
    # boost yasd
    BOOST_VERSION=1.77.0 \
    BOOST_VERSION_FILE=1_77_0 \
    # mongodb
    MONGODB_VERSION=1.12.0 \
    # 构建xlswriter扩展
    XLSWRITER_VERSION=1.5.1 \
    # inotify
    INOTIFY_VERSION=3.0.0 \
    # install and remove building packages
    PHPIZE_DEPS="autoconf dpkg-dev dpkg file g++ gcc libc-dev make php7-dev php7-pear pkgconf re2c pcre-dev pcre2-dev zlib-dev libtool automake libaio-dev openssl-dev curl-dev linux-headers" \
	TIMEZONE=${timezone:-"Asia/Shanghai"} \
	APP_ENV=prod \
    SCAN_CACHEABLE=(true) \
	# 阿里云镜像
    COMPOSER_CDN=https://mirrors.aliyun.com/composer/ \
    # 腾讯云镜像设置
    # https://mirrors.cloud.tencent.com/composer/ \
    #华为云镜像设置
    # https://mirrors.huaweicloud.com/repository/php/ \
    #cnpkg镜像设置
    # https://php.cnpkg.org \
    # Setup bento4 environment variables
    PATH=/opt/bento4/bin:${PATH}

##
# ---------- building ----------
##
RUN set -ex \
    # change apk source repo
    && echo "https://php.hernandev.com/v${ALPINE_VERSION}/php-7.4" >> /etc/apk/repositories \
    && echo "@php https://php.hernandev.com/v${ALPINE_VERSION}/php-7.4" >> /etc/apk/repositories \
    # update
    && sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
    && apk update \
    # Install dependencies
    && apk add --no-cache python3 make cmake gcc g++ boost-dev \
    # for swoole extension libaio linux-headers
    && apk add --no-cache libstdc++ openssl git bash \
    && apk add --no-cache --virtual .build-deps $PHPIZE_DEPS \
    && apk add --no-cache \
    # Install base packages ('ca-certificates' will install 'nghttp2-libs')
    ca-certificates \
    curl \
    wget \
    tar \
    xz \
    libressl \
    tzdata \
    pcre \
    php7 \
    php7-bcmath \
    php7-curl \
    php7-ctype \
    php7-dom \
    php7-gd \
    php7-iconv \
    php7-json \
    php7-mbstring \
    php7-mysqlnd \
    php7-openssl \
    php7-pdo \
    php7-pdo_mysql \
    php7-pdo_sqlite \
    php7-phar \
    php7-posix \
    php7-redis \
    php7-sockets \
    php7-sodium \
    php7-sysvshm \
    php7-sysvmsg \
    php7-sysvsem \
    php7-zip \
    php7-zlib \
    php7-xml \
    php7-xmlreader \
    php7-pcntl \
    php7-opcache \
    && ln -sf /usr/bin/php7 /usr/bin/php \
    && ln -s /usr/bin/phpize7 /usr/local/bin/phpize \
    && ln -s /usr/bin/php-config7 /usr/local/bin/php-config \
    # download
    && cd /tmp \
    #&& curl -SL "https://github.com/swoole/swoole-src/archive/${SW_VERSION}.tar.gz" -o swoole.tar.gz \
    #&& git clone https://github.com/swoole/yasd.git /tmp/yasd \
    #&& curl -fsSL "http://pecl.php.net/get/mongodb-${MONGODB_VERSION}.tgz" -o mongodb.tgz
    #&& curl -fsSL "https://pecl.php.net/get/xlswriter-${XLSWRITER_VERSION}.tgz" -o xlswriter.tgz \
    #&& curl -fsSL "https://pecl.php.net/get/inotify-${INOTIFY_VERSION}.tgz" -o inotify.tgz \
    #&& git clone https://github.com/axiomatic-systems/Bento4.git /tmp/bento4 \
    && git clone https://gitee.com/lujihong/docker.git /tmp/ \
    && ls -alh \
    # php extension:swoole
    && cd /tmp \
    && mkdir -p swoole \
    && tar -xf swoole-src-4.8.12.tar.gz -C swoole --strip-components=1 \
    && ( \
        cd swoole \
        && phpize \
        && ./configure --enable-openssl --enable-http2 --enable-swoole-curl --enable-swoole-json \
        && make -s -j$(nproc) && make install \
    ) \
    # php extension:mongodb
    && cd /tmp \
    && mkdir -p mongodb \
    && tar -xzvf mongodb.tgz -C mongodb --strip-components=1 \
    && cd mongodb \
    && phpize && ./configure --enable-mongodb \
    && make -s -j$(nproc) && make install \
    # php extension:inotify
    && cd /tmp \
    && mkdir -p inotify \
    && tar -zxvf inotify.tgz -C inotify --strip-components=1 \
    && ( \
        cd inotify \
        && phpize \
        && ./configure --enable-inotify \
        && make -s -j$(nproc) && make install \
    ) \
    # php extension:xlswriter
    && cd /tmp \
    && mkdir -p xlswriter \
    && tar -xf xlswriter.tgz -C xlswriter --strip-components=1 \
    && ( \
        cd xlswriter \
        && phpize \
        && ./configure --enable-reader \
        && make -s -j$(nproc) && make install \
    ) \
    # Build bento4
    && cd /tmp/bento4 \
    && ls -alh \
    && rm -rf /tmp/bento4/cmakebuild \
    && mkdir -p /tmp/bento4/cmakebuild/x86_64-unknown-linux \
    && cd /tmp/bento4/cmakebuild/x86_64-unknown-linux \
    && cmake -DCMAKE_BUILD_TYPE=Release ../.. \
    && make \
    # Install bento4
    && cd /tmp/bento4 && python3 Scripts/SdkPackager.py x86_64-unknown-linux . cmake \
    && mkdir /opt/bento4 \
    && mv /tmp/bento4/SDK/Bento4-SDK-*.x86_64-unknown-linux/* /opt/bento4 \
    # install composer \
    && cd /tmp \
    #&& wget -nv -O /usr/local/bin/composer https://github.com/composer/composer/releases/download/${COMPOSER_VERSION}/composer.phar \
    && mv /tmp/composer.phar /usr/local/bin/composer \
    && chmod u+x /usr/local/bin/composer \
    && composer config -g repo.packagist composer ${COMPOSER_CDN} \
	#  ---------- some config ----------
    && echo "opcache.enable_cli = 'On'" > /etc/php7/conf.d/00_opcache.ini \
    && echo "extension=swoole.so" > /etc/php7/conf.d/50_swoole.ini \
    && echo "swoole.use_shortname = 'Off'" >> /etc/php7/conf.d/50_swoole.ini \
    && echo "extension=inotify.so" > /etc/php7/conf.d/10_inotify.ini \
    && echo "extension=xlswriter.so" > /etc/php7/conf.d/01_xlswriter.ini \
    && echo "extension=mongodb.so" > /etc/php7/conf.d/50_mongodb.ini \

    # - config PHP
    && cd /etc/php7 \
    && { \
        echo "upload_max_filesize=128M"; \
        echo "post_max_size=128M"; \
        echo "memory_limit=1G"; \
        echo "date.timezone=${TIMEZONE}"; \
    } | tee conf.d/99_overrides.ini \

    # - config timezone
    && ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
    && echo "${TIMEZONE}" > /etc/timezone \

    # php info
    && php -v \
    && php -m \
    && php --ri swoole \
    && composer \

    # ---------- clear works ----------
    && apk del .build-deps \
    && apk del --purge *-dev \
    && rm -rf /var/cache/apk/* /tmp/* /usr/share/man /usr/local/bin/php* /usr/share/php7 \
    && echo -e "\033[42;37m Build Completed :).\033[0m\n"

WORKDIR /opt/www

COPY . /opt/www

# Composer Cache
# COPY ./composer.* /opt/www/
# RUN composer install --no-dev --no-scripts

#EXPOSE 9501 9502

ENTRYPOINT ["php", "/opt/www/bin/hyperf.php", "start"]
